{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WTBH 已成功更新！count: '8'\n"
     ]
    }
   ],
   "source": [
    "def get_text(markcontent):\n",
    "    isGet = False\n",
    "    returnStr = \"\"\n",
    "    specStrArr = [\"无建筑\", \"没有建筑\", \"已拆除\", \"破旧房屋\", \"房屋不存在，需要删除\", \"废弃房屋\",\n",
    "                  \"危房已拆除\", \"已拆\", \"已经拆除，需要删除\", \"临时搭建\", \"武穴城投公司临时搭建的工棚\"]\n",
    "    # 如果markcontent包含specStr, 则isGet为True, returnStr为\"123\"\n",
    "    # 如果markcontent包含鸡、牛、鸭，则isGet为True, returnStr为\"养殖棚\"\n",
    "    # 如果markcontent包含猪，则isGet为True, returnStr为\"猪圈\"\n",
    "    # 如果markcontent包含厕所、旱厕，则isGet为True, returnStr为\"厕所\"\n",
    "    if any(word in markcontent for word in specStrArr):\n",
    "        isGet = True\n",
    "        returnStr = \"废弃建筑\"\n",
    "    elif any(word in markcontent for word in [\"鸡\", \"牛\", \"鸭\", \"鱼\", \"羊\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"养殖棚\"\n",
    "    elif \"猪\" in markcontent:\n",
    "        isGet = True\n",
    "        returnStr = \"猪圈\"\n",
    "    elif any(word in markcontent for word in [\"厕所\", \"旱厕\", \"私厕\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"厕所\"\n",
    "    elif any(word in markcontent for word in [\"车棚\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"车棚\"\n",
    "    elif any(word in markcontent for word in [\"柴房\", \"柴棚\", \"临时棚\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"棚房\"\n",
    "    elif any(word in markcontent for word in [\"厨房\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"厨房\"\n",
    "    elif any(word in markcontent for word in [\"土地庙\", \"庙\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"土地庙\"\n",
    "    elif any(word in markcontent for word in [\"移动\", \"联通\", \"电信\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"通信设施\"\n",
    "    elif any(word in markcontent for word in [\"仓库\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"仓库\"\n",
    "    elif any(word in markcontent for word in [\"车库\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"车库\"\n",
    "    elif any(word in markcontent for word in [\"门卫室\"]):\n",
    "        isGet = True\n",
    "        returnStr = \"门卫室\"\n",
    "\n",
    "    return isGet, returnStr\n",
    "\n",
    "# 获取当前工程\n",
    "aprx = arcpy.mp.ArcGISProject(\"CURRENT\")\n",
    "\n",
    "# 获取活动的地图或场景\n",
    "active_map_or_scene = aprx.activeMap\n",
    "# 查找对应名称的图层\n",
    "target_layer_name = \"梅川镇_构筑物\"\n",
    "\n",
    "target_layer = None\n",
    "for lyr in active_map_or_scene.listLayers():\n",
    "    if lyr.name == target_layer_name:\n",
    "        target_layer = lyr\n",
    "        break\n",
    "\n",
    "if target_layer is None:\n",
    "    print(\"图层未找到。\")\n",
    "    exit\n",
    "\n",
    "# 获取 WTMX 和 WTBH 字段\n",
    "wtmx_field = [field for field in arcpy.ListFields(target_layer) if field.name == \"WTMX\"][0]\n",
    "wtbh_field = [field for field in arcpy.ListFields(target_layer) if field.name == \"WTBH\"][0]\n",
    "\n",
    "# 遍历图层中的所有记录\n",
    "count = 0\n",
    "with arcpy.da.UpdateCursor(target_layer, [\"WTMX\", \"WTBH\"]) as cursor:\n",
    "    for row in cursor:\n",
    "        # 检查 WTMX 是否包含 \"xx\"\n",
    "        isGet, returnStr = get_text(row[0])\n",
    "        if isGet:\n",
    "            # 更新 WTBH 为 \"123\"\n",
    "            row[1] = returnStr\n",
    "            # 更新记录\n",
    "            cursor.updateRow(row)\n",
    "            count += 1            \n",
    "            \n",
    "print(f\"WTBH 已成功更新！count: '{count}'\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ArcGISPro",
   "language": "Python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
